package zj.com.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import zj.com.adapter.GjgxAdapter;
import zj.com.adapter.StcjAdapter;
import zj.com.app.ZjApplication;
import zj.com.model.DjcModel;
import zj.com.model.JcxDB;
import zj.com.model.Org;
import zj.com.model.Project;
import zj.com.model.Stcj;
import zj.com.net.NetManager;
import zj.com.net.OnResponseListener;
import zj.com.util.DialogUtil;
import zj.com.util.LogUtils;
import zj.com.zj.R;

/**
 * Created by xi on 16/6/9.
 * 实体采集
 */
public class StcjActivity extends BaseActivity implements View.OnClickListener{


    public TextView title_name,right_tv,cancel_tv,submit_tv,del_tv,select_gs_tv,select_prj_tv,select_gx_tv;
    public ImageView left_iv;

    public ListView listView;
    public StcjAdapter adapter;
    public List<Stcj> list;

    public Intent intent;
    public LinearLayout bottom_ll;
    public TextView add_tc;
    public RelativeLayout error_rl;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stjc);

        initView();

    }

    public void initView(){

        error_rl = (RelativeLayout) findViewById(R.id.error_rl);
        add_tc = (TextView) findViewById(R.id.add_tc);
        add_tc.setVisibility(View.VISIBLE);
        add_tc.setOnClickListener(this);
        add_tc.setText("+ 实体采集情况");

        select_gs_tv = (TextView) findViewById(R.id.select_gs_tv);
        select_gs_tv.setOnClickListener(this);
        select_prj_tv = (TextView) findViewById(R.id.select_prj_tv);
        select_prj_tv.setOnClickListener(this);
        select_gx_tv = (TextView) findViewById(R.id.select_gx_tv);
        select_gx_tv.setOnClickListener(this);


        cancel_tv = (TextView) findViewById(R.id.cancel_tv);
        cancel_tv.setOnClickListener(this);
        bottom_ll = (LinearLayout) findViewById(R.id.bottom_ll);
        submit_tv = (TextView) findViewById(R.id.submit_tv);
        submit_tv.setOnClickListener(this);
        del_tv = (TextView) findViewById(R.id.del_tv);
        del_tv.setOnClickListener(this);

        title_name = (TextView) findViewById(R.id.title_name);
        title_name.setText("实体情况采集");
        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setText("编辑");
        right_tv.setVisibility(View.GONE);
        right_tv.setOnClickListener(this);
        left_iv = (ImageView) findViewById(R.id.left_iv);
        left_iv.setOnClickListener(this);
        left_iv.setVisibility(View.VISIBLE);

        listView = (ListView) findViewById(R.id.listView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                intent = new Intent(StcjActivity.this, StcjDetailActivity.class);
                intent.putExtra("stcj",list.get(position));
                startActivity(intent);
            }
        });

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

//                bottom_ll.setVisibility(View.VISIBLE);
//                right_tv.setVisibility(View.GONE);
//                adapter.setIsEdit(true);
                return false;
            }
        });

        list = new ArrayList<Stcj>();

        adapter = new StcjAdapter(list,this);
        listView.setAdapter(adapter);

    }

    public void initData(){



        List<Org> orgList = ZjApplication.orgList;
        if(orgList != null && orgList.size()> 0){

            Org org = orgList.get(0);
            select_gs_tv.setText(org.getOrgName());
             prj = ZjApplication.projectMap.get(org.getOrgCode()).get(0);
            if(prj != null){
                select_prj_tv.setText(prj.getProjectName());
                LogUtils.getInstance().error("  obtain data local");
                obtainData();
            }
            LogUtils.getInstance().error("  obtain data local project is null");

        }else{
            obtainGs();
            LogUtils.getInstance().error("  obtain data net");
        }

    }

    @Override
    protected void onResume() {
        initData();
        super.onResume();
    }

    @Override
    public void onClick(View v) {

        switch(v.getId()){
            case R.id.left_iv:
                finish();
                break;
            case R.id.right_tv:
                bottom_ll.setVisibility(View.VISIBLE);
                right_tv.setVisibility(View.GONE);
                adapter.setIsEdit(true);
                break;
            case R.id.cancel_tv:

                bottom_ll.setVisibility(View.GONE);
                right_tv.setVisibility(View.VISIBLE);
                adapter.setIsEdit(false);

                break;
            case R.id.submit_tv:
                bottom_ll.setVisibility(View.GONE);
                right_tv.setVisibility(View.VISIBLE);
                adapter.setIsEdit(false);

                break;
            case R.id.del_tv:
                bottom_ll.setVisibility(View.GONE);
                right_tv.setVisibility(View.VISIBLE);
                adapter.setIsEdit(false);
                break;
            case R.id.select_gs_tv:
                intent = new Intent(this,SelectDoubleListActivity.class);
                startActivityForResult(intent, GS_XM_CODE);
                break;
            case R.id.select_prj_tv:
                intent = new Intent(this,SelectDoubleListActivity.class);
                startActivityForResult(intent, GS_XM_CODE);
                break;
            case R.id.select_gx_tv:
                intent = new Intent(StcjActivity.this,SelectInputActivity.class);
                startActivityForResult(intent,SEARCH_CODE);
                break;
            case R.id.add_tc:
                intent = new Intent(this,StcjAddlActivity.class);
                startActivity(intent);

                break;
        }
    }


    public List<Org> orgList;
    public Project selectProject;
    public Org selectOrg;
    public final int GS_XM_CODE = 0x2210;
    public final int SEARCH_CODE = GS_XM_CODE+1;
    public ProgressDialog dialog;
    public void obtainGs(){

        dialog = DialogUtil.createProgressDialog(this, "提示", "数据获取中");
        NetManager.getInstance(this).QueryOrgs(new OnResponseListener<List<Org>>() {
            @Override
            public void onComplete(boolean isOK, List<Org> result, int errorCode, String errorMsg) {

                if (isOK) {
                    orgList = result;
                    if (orgList != null && orgList.size() > 0) {

                        Org org = orgList.get(0);
                        ZjApplication.orgList = orgList;
                        select_gs_tv.setText(org.getOrgName());
                        obtainProject(org);
                    }
                } else {
                    Toast.makeText(StcjActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();
                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {

                dialog.dismiss();
                Toast.makeText(StcjActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void obtainProject(final  Org org){

        NetManager.getInstance(this).GetProjectList(org.getOrgCode(), new OnResponseListener<List<Project>>() {
            @Override
            public void onComplete(boolean isOK, List<Project> result, int errorCode, String errorMsg) {

                if (isOK) {

                    if (result != null && result.size() > 0) {

                        prj = result.get(0);
                        select_prj_tv.setText(prj.getProjectName());
                        ZjApplication.projectMap.put(org.getOrgCode(), result);
                        obtainData();
                    }else{
                        dialog.dismiss();
                    }



                } else {
                    Toast.makeText(StcjActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
                    dialog.dismiss();

                }
            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                Toast.makeText(StcjActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
    }


    public  int pageIndex = 1;
    public String keyword = "";
    public  Project prj;
    public void obtainData(){

        if(dialog == null) {
            dialog = DialogUtil.createProgressDialog(this,"提示","数据获取中...");
        }

        NetManager.getInstance(this).GetEntityProjectList(prj.getProjectID()+"", keyword, pageIndex+"", new OnResponseListener<List<Stcj>>() {
            @Override
            public void onComplete(boolean isOK, List<Stcj> result, int errorCode, String errorMsg) {


                if(isOK){

                    if(result != null && result.size() > 0){
                        list = result;
                        error_rl.setVisibility(View.GONE);
                    }else{
                        error_rl.setVisibility(View.VISIBLE);

                    }
                    adapter.setDataChange(list);

                }else{
                    Toast.makeText(StcjActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
                }
                dialog.dismiss();

            }

            @Override
            public void onInternError(int errorCode, String errorMessage) {
                error_rl.setVisibility(View.VISIBLE);

                Toast.makeText(StcjActivity.this, "获取失败", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == RESULT_OK){

            if(requestCode == GS_XM_CODE){

                selectProject = (Project) data.getSerializableExtra("project");
                selectOrg = (Org) data.getSerializableExtra("org");
                if(selectProject != null){

                    select_prj_tv.setText(selectProject.getProjectName());
                }
                if(selectOrg != null){
                    select_gs_tv.setText(selectOrg.getOrgName());
                }
            }else if(requestCode == SEARCH_CODE){

                keyword = data.getStringExtra("keyword");
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
